package com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.OrderNoticeEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.mapper.OrderNoticeMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.StorerecordEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.EpclistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.StorerecordEntityMapperImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderNoticeService {
    @Autowired
    private OrderNoticeMapperImpl orderNoticeMapper;

    @Autowired
    StorerecordEntityMapperImpl tempStorerecordEntityMapper;

    @Autowired
    EpclistMapperImpl tempEpclistMapper;

    public OrderNoticeEntity selectById(Integer id) {
        return orderNoticeMapper.selectByPrimaryKey(id);
    }

    public List<OrderNoticeEntity> SelectByBatchno(String orderid, String batchno, int orderType, String tenant_id) {
        return orderNoticeMapper.selectByBatchno(orderid, batchno, orderType, tenant_id);
    }

    public List<OrderNoticeEntity> SelectByOrderType(int orderType, String tenant_id) {
        return orderNoticeMapper.selectByOrderType(orderType, tenant_id);
    }

    public List<OrderNoticeEntity> SelectByType(List<String> strList, String transtype, int orderType, String tenant_id) {
        return orderNoticeMapper.selectByType(strList, transtype, orderType, tenant_id);
    }

    public int DelEntitys(List<Integer> ids, String tenant_id) {
        int rInt = orderNoticeMapper.editByPrimaryKeys(ids, tenant_id);
        return rInt;
    }

    public int InsertEntity(OrderNoticeEntity record) {
        int rInt = orderNoticeMapper.insert(record);
        return rInt;
    }

    public int InsertBatch(List<OrderNoticeEntity> records) {
        if (records.size() == 0) return 0;
        int rInt = orderNoticeMapper.insertBatch(records);
        return rInt;
    }

    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(OrderNoticeEntity record, List<StorerecordEntity> datalist, String tenant_id) throws Exception {
        // 更新记录
        int rInt_storerecord = tempStorerecordEntityMapper.EditEntity(datalist, tenant_id);
        int rInt = orderNoticeMapper.insert(record);
        return rInt;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table NewTable
     *
     * @mbg.generated Mon Mar 07 07:40:31 UTC 2022
     */
    public int updateByPrimaryKeySelective(OrderNoticeEntity record) {
        return orderNoticeMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 单据状态编辑
     *
     * @param orderid
     * @param type
     * @param status
     * @param modifier
     * @param auditstatus
     * @param pickbatchno
     * @return
     */
    public int EditEntityStatus(String orderid, int type, int status, String modifier, int auditstatus, String pickbatchno, String tenantId) {
        return orderNoticeMapper.EditEntityStatus(orderid, type, status, modifier, auditstatus, pickbatchno, tenantId);
    }

    public int EditEntityStatusForPick(String orderid, int type, int repickstatus, int auditstatus, String modifier, String pickbatchno, String tenantId) {
        return orderNoticeMapper.EditEntityStatusForPick(orderid, type, repickstatus, pickbatchno, auditstatus, modifier, tenantId);
    }

    /**
     * 审核状态编辑
     *
     * @param orderid
     * @param type
     * @param auditstatus
     * @param modifier
     * @return
     */
    public int EditEntityAuditstatus(String orderid, String pickbatchno, int type, int auditstatus, String modifier, String tenantId) {
        return orderNoticeMapper.EditEntityAuditstatus(orderid, type, auditstatus, modifier, pickbatchno, tenantId);
    }

    /**
     * 检查更新单据状态
     *
     * @param entity              当前配货单
     * @param storerecordEntities 所有当前配货记录
     * @param tenantId
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int CheckAuditstatusByEntity(OrderNoticeEntity entity, List<StorerecordEntity> storerecordEntities, String tenantId, String userId) {
        if (entity == null) return 0;
        List<String> epcOther = new ArrayList<>();
        if (!StringUtils.isEmpty(entity.getRemarkdxq())) {
            JSONObject jsonObject = JSONObject.parseObject(entity.getRemarkdxq());
            epcOther = Arrays.asList(jsonObject.getString("epcIds").split(","));
        }
        //更新库存物料状态 EPCs
        String allotStatus = String.valueOf(DxqConfig.AllotOk);
        if (storerecordEntities.size() > 0) {
            List<String> ids = storerecordEntities.stream().map(it -> it.getId()).collect(Collectors.toList());
            List<String> epcs = storerecordEntities.stream().map(it -> it.getRfidcode()).collect(Collectors.toList());
            tempEpclistMapper.editStatusForAllot(epcs, tenantId, userId, allotStatus);// 已拣货转已配货
            tempStorerecordEntityMapper.EditStatus(ids, tenantId, userId, allotStatus, entity.getPickbatchno());// 已拣货转已配货
        }
        return 1;
    }

    public int updateByPrimaryKey(OrderNoticeEntity entity) {
        return orderNoticeMapper.updateByPrimaryKey(entity);
    }
}
